package com.yy.wga.mp.module.income.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yy.wga.mp.module.income.response.IncomeUserBalanceInfoResponse;
import com.yy.wga.mp.common.aop.NotLogin;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.common.StringUtils;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.income.entity.pojo.IncomeOrderGoodsDO;
import com.yy.wga.mp.module.income.entity.pojo.IncomeUserBalanceDO;
import com.yy.wga.mp.module.income.entity.pojo.IncomeWithdrawSettingDO;
import com.yy.wga.mp.module.income.request.IncomeUserBalanceWithdrawRequest;
import com.yy.wga.mp.module.income.service.IncomeOrderGoodsService;
import com.yy.wga.mp.module.income.service.IncomeUserBalanceService;
import com.yy.wga.mp.module.income.service.IncomeWithdrawSettingService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 用户余额
 *
 * @author guochao
 * @since 2024-11-12 00:27:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("api/income/userBalance")
public class IncomeUserBalanceController {
    /**
     * 服务对象
     */
    private final IncomeUserBalanceService incomeUserBalanceService;
    private final IncomeOrderGoodsService incomeOrderGoodsService;
    private final IncomeWithdrawSettingService withdrawSettingService;


    @NotLogin
    @GetMapping("openWithdrawal")
    public Result<Boolean> withdrawal() {
        return Result.success(false);
    }

    /**
     * 获取余额
     * @return
     */
    @PostMapping("get")
    public Result<IncomeUserBalanceInfoResponse> get() {
        IncomeUserBalanceDO userBalanceDO = incomeUserBalanceService.getOne(new LambdaQueryWrapper<IncomeUserBalanceDO>()
                .eq(IncomeUserBalanceDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .eq(IncomeUserBalanceDO::getUserId, RequestUserHolder.getUserCache().getUserId()));
        if (userBalanceDO == null) {
            return Result.success(new IncomeUserBalanceInfoResponse());
        }
        IncomeUserBalanceInfoResponse response = new IncomeUserBalanceInfoResponse();
        response.setSettle(userBalanceDO.getSettle());
        response.setTotal(userBalanceDO.getSettle() + userBalanceDO.getInWithdraw() + userBalanceDO.getWithdraw());
        response.setUnSettle(userBalanceDO.getUnSettle());

        // 今日结算
        List<IncomeOrderGoodsDO> incomeOrderGoodsList = incomeOrderGoodsService.list(new LambdaQueryWrapper<IncomeOrderGoodsDO>()
                .eq(IncomeOrderGoodsDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .eq(IncomeOrderGoodsDO::getUserId, RequestUserHolder.getUserCache().getUserId())
                .ge(IncomeOrderGoodsDO::getSettleTime, new Date()));
        if (!CollectionUtils.isEmpty(incomeOrderGoodsList)) {
            int sum = incomeOrderGoodsList.stream()
                    .mapToInt(IncomeOrderGoodsDO::getCommissionValue)
                    .sum();
            response.setTodaySettle(sum);
        }
        IncomeWithdrawSettingDO withdrawSetting = withdrawSettingService.getOne(new LambdaQueryWrapper<IncomeWithdrawSettingDO>()
                .eq(IncomeWithdrawSettingDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey()));
        BeansUtils.copyProperties(withdrawSetting, response);
        return Result.success(response);
    }

    /**
     * 提现
     * @return
     */
    @PostMapping("withdraw")
    public Result<Boolean> withdraw(@RequestBody @Valid IncomeUserBalanceWithdrawRequest request) {
        if (request.getType().equals(2)) {
            if (StringUtils.isBlank(request.getWithdrawAccount()) || StringUtils.isBlank(request.getRealName())) {
                return Result.fail("请输入提现账号");
            }
        }
        incomeUserBalanceService.withdraw(RequestUserHolder.getUserCache().getUserId(), request);
        return Result.success(Boolean.TRUE);
    }
}
